package com.sakura.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sakura.eduservice.entity.EduChapter;
import com.sakura.eduservice.entity.EduVideo;
import com.sakura.eduservice.entity.chapter.ChapterVo;
import com.sakura.eduservice.entity.chapter.VideoVo;
import com.sakura.eduservice.mapper.EduChapterMapper;
import com.sakura.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.eduservice.service.EduVideoService;
import com.sakura.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;


    /**课程大纲列表,根据课程id进行查询*/
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 根据课程id查询课程里面所有的章节
        List<EduChapter> eduChapterList = baseMapper
                .selectList(new QueryWrapper<EduChapter>().eq("course_id", courseId));

        //2 根据课程id查询课程里面所有的小节
        List<EduVideo> eduVideoList = videoService
                .list(new QueryWrapper<EduVideo>().eq("course_id", courseId));

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3 遍历查询章节List集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter对象赋值到ChapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);

            //创建集合,用于封装章节的小节
            List<VideoVo> videoVoList = new ArrayList<>();

            //4 遍历查询小节list集合,进行封装
            for (int m=0;m<eduVideoList.size();m++){
                //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：小节里面的chapterId和章节里的id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId章节id查询小节表，如果查询数据不进行删除
        int count = videoService.count(new QueryWrapper<EduVideo>()
                .eq("chapter_id", chapterId));
        //判断
        if (count>0){
            //查询出小节,不进行删除
            throw new GuliException(20001,"不能删除");
        }else {
            //不能查询出数据,进行删除
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        baseMapper.delete(new QueryWrapper<EduChapter>()
                .eq("course_id",courseId));
    }
}
